package com.lemon.exam.common.redis.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import reactor.core.publisher.Mono;

/**
 * Redis ID 操作类 对象
 *
 * @author Lemon
 * @since 2025/7/4 18:12
 */
public final class IdOperations extends BaseOperations {
    public IdOperations(ObjectMapper objectMapper, ReactiveRedisTemplate<String, Object> template) {
        super(objectMapper, template);
    }

    /**
     * 获取ID
     *
     * @param key key
     * @return ID
     */
    public Mono<Long> get(String key) {
        return withRetry(
                template.opsForValue().increment(key),
                "redis id increment key: %s".formatted(key)
        ).onErrorResume(err -> Mono.empty());
    }
}
